﻿using E_Surfing.WP8Framework;
using Game77SharedLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Media.Imaging;

namespace E_Surfing.SFReaderWP8.Model.Http
{
    public class ImageThreadPool
    {
        /// <summary>
        /// 图片下载回调事件
        /// </summary>
        public ReaderImage.ResponseImageEvent responseImageEvent;
        /// <summary>
        /// 封面名字对应的书本ID
        /// </summary>
        public string imageNameForContentId = string.Empty;
        /// <summary>
        /// 是否需要保存
        /// </summary>
        private bool isSave = true;

        /// <summary>
        /// 是否保存在独立的目录
        /// </summary>
        private bool isSaveInDir = false;

        /// <summary>
        /// 保存目录名
        /// </summary>
        private string saveDirName = string.Empty;

        private bool useDefaultBitmap = true;

        /// <summary>
        /// 下载加入到线程
        /// </summary>
        /// <param name="imageEvent"></param>
        /// <param name="imageUrl"></param>
        /// <param name="contentId"></param>
        public void DownloadImageToThreadPool(ReaderImage.ResponseImageEvent imageEvent, string imageUrl, string contentId, bool useDefault = true)
        {
            imageNameForContentId = contentId;
            responseImageEvent = imageEvent;
            useDefaultBitmap = useDefault;

            ThreadPool.QueueUserWorkItem((iaa) =>
            {   
                //get
                WPWebClient webClient = new WPWebClient();
                WPRequestData rData = new WPRequestData()
                {
                    requestMethod = RequestMethod.METHOD_GET,
                    httpUrl = imageUrl,
                    isNeedCache = false,
                    timeOut = "10",
                };
                webClient.OnOpenReadCompleted += OnOpenReadImageCompleted;
                webClient.Connection(rData, false);
            });
        }

        /// <summary>
        /// 请求图片下载
        /// </summary>
        /// <param name="threadContext"></param>
        public void DownloadImageThreadPoolCallback(Object threadContext)
        {
            string imageUri = (string)threadContext;

            //get
            WPWebClient webClient = new WPWebClient();
            WPRequestData rData = new WPRequestData()
            {
                requestMethod = RequestMethod.METHOD_GET,
                httpUrl = imageUri,
                isNeedCache = false,
                timeOut = "10",
            };
            webClient.OnOpenReadCompleted += OnOpenReadImageCompleted;
            webClient.Connection(rData, false);
        }

        /// <summary>
        /// 返回流的响应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnOpenReadImageCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            WebClient wcSender = (WebClient)sender;

            try
            {
                if (e.Result != null)
                {
                    if (isSave)
                    {
                        //获取下载的流数据(在此处是图片数据)中
                        byte[] bytes = new byte[e.Result.Length];
                        e.Result.Read(bytes, 0, bytes.Length);
                        ReaderImage.SaveImage(bytes, imageNameForContentId);
                    }

                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(e.Result);
                    this.responseImageEvent(bitmapImage, imageNameForContentId);
                    return;
                }
                if (useDefaultBitmap)
                {
                    this.responseImageEvent(GlobalUnit.sDefaultBitmap, imageNameForContentId);
                }
            }
            catch
            {
                //返回默认的图片
                if (useDefaultBitmap)
                {
                    this.responseImageEvent(GlobalUnit.sDefaultBitmap, imageNameForContentId);
                }
            }
        }
    }
}
